﻿function getUrlVars(url)
{
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

(function(){CKEDITOR.dialog.add('youtube',
	function(editor)
	{return{title:editor.lang.youtube.title,minWidth:CKEDITOR.env.ie&&CKEDITOR.env.quirks?368:350,minHeight:240,
	onShow:function(){
		this.getContentElement('general','content').getInputElement().setValue('');
		this.getContentElement('general','width').getInputElement().setValue('560');
		this.getContentElement('general','height').getInputElement().setValue('315')
	},
	onOk:function(){
		       		val = this.getContentElement('general','content').getInputElement().getValue();
		       		width = this.getContentElement('general','width').getInputElement().getValue();
		       		width = parseInt(width) ? parseInt(width) : 560;
		       		height = this.getContentElement('general','height').getInputElement().getValue();
		       		height = parseInt(height) ? parseInt(height) : 315;
       				new_url = getUrlVars(val);
					var text='<iframe width="'+width+'" allowfullscreen="" frameborder="0" height="'+height+'" src="http://www.youtube.com/embed/'
					+ new_url["v"]
					+'?rel=0" frameborder="0"></iframe>';
	this.getParentEditor().insertHtml(text)},
	contents:[{label:editor.lang.common.generalTab,id:'general',elements:
																		[{type:'html',id:'pasteMsg',html:'<div style="white-space:normal;width:500px;"><img style="margin:5px auto;" src="'
																		+CKEDITOR.getUrl(CKEDITOR.plugins.getPath('youtube')
																		+'images/youtube_large.png')
																		+'"><br />'+editor.lang.youtube.pasteMsg
																		+'</div>'},
																		{type:'html', id:'content',style:'width:340px;height:90px',html:'<input size="100" style="'+'border:1px solid black;'+'background:white">',focus:function(){this.getElement().focus()}},
																		{type: 'hbox', children: [
																		                          	{type: 'vbox', children: [
																		                          	{type:'html', html:'Width:',focus:function(){this.getElement().focus()}},
																		                          	{type:'html', id:'width', html:'<input size="10" style="'+'border:1px solid black;'+'background:white">',focus:function(){this.getElement().focus()}}
																		                          	]},
																		                          	{type: 'vbox', children: [
																		                          	{type:'html', html:'Height:',focus:function(){this.getElement().focus()}},
																									{type:'html', id:'height',style:'width:340px;height:90px',html:'<input size="10" style="'+'border:1px solid black;'+'background:white">',focus:function(){this.getElement().focus()}}
																		                          	]}
																		]},
																		]}]}})})();
